/*
* $Id: FamiliesTable.cpp 153 2010-09-26 17:33:07Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/#include "Common/include.h"
#include "FamiliesTable.h"
#include "TableNames.h"
#include "../SqlBinder.h"

CFamiliesTable::CFamiliesTable()
: CTable(FAMILIES_TABLE_NAME){}
CFamiliesTable::~CFamiliesTable(){}

void CFamiliesTable::CreateTable(IDatabase* pDatabase){
	std::tstring strRequest(_T("CREATE TABLE IF NOT EXISTS "));
	pDatabase->ExecRequest(strRequest + GetNameTable() + 
						_T("(id_family INTEGER NOT NULL,")\
						_T("name TEXT NOT NULL,")\
						_T("id_point INTEGER NOT NULL,")\
						_T("type INTEGER NULL,")\
						_T("note TEXT NULL,")\
						_T("id_record INTEGER NULL,")\
						_T("PRIMARY KEY(id_family ASC AUTOINCREMENT),")\
						_T("CONSTRAINT fkFamiliesPoint")\
						_T("	FOREIGN KEY(id_point)")\
						_T("	REFERENCES points(id_point),")\
						_T("CONSTRAINT fkFamiliesRecord")\
						_T("	FOREIGN KEY(id_record)")\
						_T("	REFERENCES field_values(id_record)")\
						_T("	ON DELETE SET NULL);"));
}
void CFamiliesTable::UpdateValue(IDatabase* pDatabase){
	std::tstring strRequest(_T("UPDATE "));
	CSqlBinder binder(strRequest + GetNameTable() + 
					_T(" SET name=?, id_point=?, type=?, note=?, id_record=?")\
					_T(" WHERE id_family=?"));
	binder.SetValue(1, m_content.s_type);
	binder.SetValue(0, m_content.s_strName);
	binder.SetValue(2, m_content.s_strNote);
	binder.SetValue(3, m_content.s_nIdRecord);
	binder.SetValue(4, m_content.s_nId);
	pDatabase->ExecRequest(binder.GetRequest());
	m_content.Clear();
}
IResult* CFamiliesTable::GetValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("SELECT * FROM "));
	if(m_content.s_nId){
		binder.SetRequest(strRequest + GetNameTable() + _T(" WHERE id_family=?"));
		binder.SetValue(0, m_content.s_nId);
	}else if(m_content.s_nIdPoint){
		binder.SetRequest(strRequest + GetNameTable() + _T(" WHERE id_point=?"));
		binder.SetValue(0, m_content.s_nIdPoint);
	}else
		binder.SetRequest(strRequest + GetNameTable());
	m_content.Clear();
	return FillContent(pDatabase->ExecRequest(binder.GetRequest()));
}
void CFamiliesTable::SetValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("INSERT INTO "));
	binder.SetRequest(strRequest + GetNameTable() + 
					_T("(name, id_point, type, note, id_record)")\
					_T(" VALUES(?, ?, ?, ?, ?)"));
	binder.SetValue(0, m_content.s_strName);
	binder.SetValue(1, m_content.s_nIdPoint);
	binder.SetValue(2, m_content.s_type);
	binder.SetValue(3, m_content.s_strNote);
	binder.SetValue(4, m_content.s_nIdRecord);
	pDatabase->ExecRequest(binder.GetRequest());
	strRequest = _T("SELECT MAX(id_family) as id_family FROM ");
	MyTable table = pDatabase->ExecRequest(strRequest + GetNameTable());
	if(table.empty())
		m_content.s_nId = 0;
	else
		m_content.s_nId = _ttoi(table[0][0].m_strValueColumn.c_str());
}
void CFamiliesTable::DeleteValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("DELETE FROM "));
	if(m_content.s_nId){
		strRequest = strRequest + GetNameTable() + _T(" WHERE id_family=?");
		binder.SetValue(0, m_content.s_nId);
		if(!m_content.s_strName.empty()){
			strRequest = strRequest + _T(" AND name=?");
			binder.SetValue(1, m_content.s_strName);
		}
		binder.SetRequest(strRequest);
	}else{
		binder.SetRequest(strRequest + GetNameTable());
	}
	m_content.Clear();
	pDatabase->ExecRequest(binder.GetRequest());
}
IOperation::i_content* CFamiliesTable::CreateContent(){
	return new content;
}

CFamiliesTable::content::content(){
	Clear();
}
void CFamiliesTable::content::Clear(){
	s_nId = 0;
	s_strName.clear();
	s_nIdRecord = 0;
	s_strNote.clear();
	s_type = Family::DEFAULT;
	s_nIdPoint = 0;
}
void CFamiliesTable::content::Set(const std::vector<CTableItem>& aContent){
	for(std::vector<CTableItem>::const_iterator it = aContent.begin(); it != aContent.end(); it++){
		if(it->m_strNameColumn == _T("id_family"))
			s_nId = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("note"))
			s_strNote = it->m_strValueColumn;
		else if(it->m_strNameColumn == _T("id_point"))
			s_nIdPoint = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("type"))
			s_type = (Family::type)_ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("id_record"))
			s_nIdRecord = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("name"))
			s_strName = it->m_strValueColumn;
	}
}
void CFamiliesTable::content::Free(){
	delete this;
}
